package com.yao.controller;

import com.yao.pojo.*;
import com.yao.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/article")
@Api(tags = "文章相关接口")
public class ArticleController {

    private ArticleService articleService;

    @Autowired
    public void ArticleController(ArticleService articleService){
        this.articleService = articleService;
    }

    /**
     * 发布文章
     * @return
     */
    @PostMapping
    @ApiOperation("发布文章")
    public Result addArticle(@RequestBody Article article){
        articleService.addArticle(article);
        return Result.success("发布成功");
    }

    /**
     * 修改文章
     * @return
     */
    @PutMapping
    @ApiOperation("修改文章")
    public Result changeArticle(@RequestBody Article article){
        boolean flag = articleService.changeArticle(article);

        if (!flag) return Result.error("暂无权限");
        return Result.success("修改文章文章成功");
    }

    /**
     * 根据文章ID删除文章
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除文章")
    public Result deleteArticle(Integer id){
        boolean flag = articleService.deleteArticle(id);
        if (!flag) return Result.error("暂无权限");
        return Result.success("删除成功");
    }

    /**
     * 文章条件查询
     * @param articleQueryVO
     * @return
     */
    public Result<PageResult> conditionQuery(ArticleQueryVO articleQueryVO){
        PageResult pageResult = articleService.conditionQuery(articleQueryVO);
        return Result.success(pageResult);
    }
}
